package com.yunchang.springboot.utils.databaseutils;

import com.alibaba.druid.util.StringUtils;
import com.yunchang.springboot.mapper.base.database.BaseDatabaseMapper;
import com.yunchang.springboot.mapper.relation.database.ShopDatabaseRelationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author chenzw
 * @date 2020/8/4 0004  14:08
 * @description:
 */
@Service
@Slf4j
public class DatabaseUtil {
    /**
     * * 店铺与数据库关联关系
    */
    private ShopDatabaseRelationMapper shopDatabaseRelationMapper;

    @Autowired
    public void setShopDatabaseRelationMapper(ShopDatabaseRelationMapper shopDatabaseRelationMapper){
        this.shopDatabaseRelationMapper = shopDatabaseRelationMapper;
    }

    /**
     * 数据库基础信息表
     */
    private BaseDatabaseMapper baseDatabaseMapper;

    @Autowired
    public void setBaseDatabaseMapper(BaseDatabaseMapper baseDatabaseMapper){
        this.baseDatabaseMapper = baseDatabaseMapper;
    }

    /**
     * 根据sellerId获取databaseName
     * @param sellerId
     * @return
     */
    public synchronized String getDatabaseNameBySellerId(String sellerId){
        if(StringUtils.isEmpty(sellerId)){
            log.warn("根据sellerId获取数据库名称时出错，sellerId为空。sellerId: "+sellerId);
            return null;
        }
        String databaseName = baseDatabaseMapper.selectDatabaseNameBySellerId(sellerId);
        if(StringUtils.isEmpty(databaseName)){
            log.warn("没有该sellerId对应的数据库名称，sellerId: "+sellerId);
            return null;
        }
        return databaseName;
    }

}
